package gov.pbc.nn.kjgl.maintenance.job;

import gov.pbc.nn.core.dao.CommonHibernateDao;
import gov.pbc.nn.core.dao.CommonJdbcDao;
import gov.pbc.nn.core.utils.EntityRowMapperUtils;
import gov.pbc.nn.kjgl.maintenance.entity.SysInspectionTask;
import gov.pbc.nn.kjgl.maintenance.entity.SysOperationStaff;
import gov.pbc.nn.kjgl.maintenance.view.SysManagePR;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.hibernate.Session;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.bstek.bdf2.core.model.Message;

/**
 * 重大时期巡检任务提醒
 * @author Administrator
 *
 */
@Component(SysZdsqxjJob.BEAN_ID)
public class SysZdsqxjJob implements Job {
	public static final String BEAN_ID = "gov.pbc.nn.kjgl.maintenance.job.SysZdsqxjJob";

	@Resource(name = CommonHibernateDao.BEAN_ID)
	private CommonHibernateDao hibernateDao;
	@Resource(name = CommonJdbcDao.BEAN_ID)
	private CommonJdbcDao jdbcDao;
	@Autowired
	private SysManagePR sysManagePR;

	private final static String SENDER = "系统自动";

	@Override
	public void execute(JobExecutionContext arg0) throws JobExecutionException {
		// TODO Auto-generated method stub
		String sql = "select * from t_sys_inspection_task t where CURDATE()>=DATE_FORMAT(t.START_DATE_,'%Y-%m-%d') and CURDATE()<=t.END_DATE_ and INVALID_=0 ";
		List<SysInspectionTask> sysIws = this.jdbcDao.getJdbcTemplate().query(sql, new RowMapper<SysInspectionTask>() {
			@Override
			public SysInspectionTask mapRow(ResultSet rs, int rowNum) throws SQLException {
				// TODO Auto-generated method stub
				return EntityRowMapperUtils.get(SysInspectionTask.class, rs, rowNum);
			}
		});
		if (sysIws.size() > 0) {
			Session session = this.hibernateDao.getSession();
			for (SysInspectionTask siw : sysIws) {
				List<SysOperationStaff> sofs = sysManagePR.loadOperationStaff(siw.getSysId());
				if (sofs.size() > 0) {
					for (SysOperationStaff sof : sofs) {

						Message msg = new Message();
						msg.setId(UUID.randomUUID().toString());
						msg.setSendDate(new Date());
						msg.setSender(SENDER);
						msg.setReply(false);
						msg.setRead(false);
						msg.setTitle("请录入今日的重大时期巡检记录");
						msg.setContent("请" + sof.getCname() + "对【+" + siw.getSysName() + "】系统进行今日的重大时期巡检记录维护");
						msg.setReceiver(sof.getUsername());
						session.save(msg);
					}
				}
			}
		}

	}
}
